import java.io.PrintWriter;
import java.util.*;

public class XMLParser {
	private Stack<NodeData> stack;
	
	private int tag;
	
	public XMLParser(){
		stack=new Stack<NodeData>();
		
		tag=0;
	}
	
	public void parseTokens(Scanner file){
		
		
		
		String input="";
		String open="";
		String data="";
		NodeData theNode=null;
		while(file.hasNext()){
			
			input=file.next();
			
			
			
			if(isOpenTag(input))
			{
				
				open+="\nXML Parser: ";
				for(int i=0; i<tag; i++){
					open+="X";
				}
				data+=" tag "+tag+" '"+input+"': '";
				
				theNode=new NodeData(data);
				//stack.push(input);
				tag++;
				/*
				if(file.next().charAt(0)!='<'){
				
				data+="\nXML Parser: ";
				for(int i=0; i<tag; i++){
					data+="X";
				}
				data+=" tag "+tag+" '"+input+"': '";
				}
				*/
				
				
			}
			else if(isCloseTag(input))
			{
				while(!isOpenTag(stack.peek())){
					stack.pop();
					
				}
					if(isOpenTag(stack.peek())){
						stack.pop();
					}
					tag--;
					
			}
				else {
				//stack.push(input);
				data+=""+input+" ";
				
			}
			
			
			
			
			
			
		}
		
		
	}
	public boolean isCloseTag(String data){
		if(data.charAt(0)=='<' && data.charAt(1)=='/' && data.charAt(data.length()-1)=='>'){
			return true;
		}
		else{
			return false;
		}
		
	}
	public boolean isOpenTag(String data){
		if(data.charAt(0)=='<' && data.charAt(data.length()-1)=='>' && data.charAt(1)!='/'){
			return true;
		}
		else{
			return false;
		}
	}
public boolean checkValidXML(Scanner inFile){
	PrintWriter output=null;
	
	output=FileUtils.PrintWriterOpen("zlog");
			this.parseTokens(inFile);
		System.out.println(data);
		output.print(data);
		output.close();
		return true;
	}

}
